using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using  UnityEngine.UI;

/// <summary>
/// 物品
/// </summary>
public class BackpackItem : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    private BackpackManager backpack;
    public int row;  // 行  
    public int col;  // 列
    private RectTransform rectTransform;
    private Image img;
    private Vector4[,] idxData;
    private float oneSize = 100;  // 单个格子大小 
    private int selectRow;  // 拖拽时选中的位置
    private int selectCol;


    public void InitView(BackpackManager pManager)
    {
        this.backpack = pManager;
    }

    void Start()
    {
        this.rectTransform = this.transform.GetComponent<RectTransform>();
        this.img = this.transform.GetComponent<Image>();
        InitIdxData();
    }

    /// <summary>
    /// 初始化物品每个格子占的位置边界
    /// </summary>
    private void InitIdxData()
    {
        // 把item 坐标原点放在左上角
        this.idxData = new Vector4[row, col];
        float x, y, z, w;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                y = i * oneSize;
                w = (i + 1) * oneSize;
                x = j * oneSize;
                z = (j + 1) * oneSize;
                this.idxData[i,j] = new Vector4(x,y,z,w);
            }
        }
    }

    public void OnBeginDrag(PointerEventData eventData)
    {
        if (this.backpack)
        {
            this.backpack.StartDrag(eventData,this);
        }

        CheckDragIdx(eventData);
    }

    public void OnDrag(PointerEventData eventData)
    {
        if (this.backpack)
        {
            this.backpack.InDrag(eventData,this);
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        if (this.backpack)
        {
            this.backpack.EndDrag(eventData,this);
        }
    }

    /// <summary>
    /// 开始拖拽的时候获取当前点击的格子位置
    /// </summary>
    /// <param name="eventData"></param>
    private void CheckDragIdx(PointerEventData eventData)
    {
        var x = eventData.position.x - this.rectTransform.position.x;
        var y = this.rectTransform.position.y - eventData.position.y;
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                var pos = this.idxData[i, j];
                if (x >= pos.x && x <= pos.z && y >= pos.y && y <= pos.w)
                {
                    selectRow = i;
                    selectCol = j;
                    break;
                }
            }
        }
    }

    /// <summary>
    /// 获取当前拖拽的位置
    /// </summary>
    /// <param name="row"></param>
    /// <param name="col"></param>
    public void GetCurSelectRowCol(out int row,out int col)
    {
        row = this.selectRow;
        col = this.selectCol;
    }

    /// <summary>
    /// 获取物品的大小
    /// </summary>
    public void GetItemRowCol(out int row,out int col)
    {
        row = this.row;
        col = this.col;
    }

    public Sprite GetSprite()
    {
        return this.img.sprite;
    }

    /// <summary>
    /// 塞下物品需要的格子数量
    /// </summary>
    /// <returns></returns>
    public int GetNeedGrid()
    {
        return this.row * this.col;
    }
}
